package com.yx.front.manager;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.front.config.global.BizException;
import com.yx.front.config.global.CommonResult;
import com.yx.front.model.CommentsReply;
import com.yx.front.model.SysUser;
import com.yx.front.model.param.CommentsParam;
import com.yx.front.model.param.CommentsReplyParam;
import com.yx.front.model.vo.CommentsReplyVo;
import com.yx.front.service.CommentsReplyService;
import com.yx.front.service.SysUserService;
import com.yx.front.utils.LoginUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.yx.front.converter.CommentsReplyConverter.handlerResult;

/**
 * @author YangXiong
 */
@Component
public class CommentsReplyManager {
    @Resource
    private CommentsReplyService commentsReplyService;
    @Resource
    private LoginUtil loginUtil;
    @Resource
    private SysUserService sysUserService;

    public CommonResult<List<CommentsReplyVo>> getPageByMainId(CommentsParam param) {
        Page<CommentsReply> replyPage = new Page<>(param.getCurrentPage(), param.getTotalPage());
        LambdaQueryWrapper<CommentsReply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CommentsReply::getCommentId, param.getMainId());
        Page<CommentsReply> page = commentsReplyService.page(replyPage, lambdaQueryWrapper);
        List<CommentsReplyVo> result = handlerResult(page.getRecords());
        return CommonResult.success(result, page.getTotal());
    }

    public Boolean insert(CommentsReplyParam param) {
        if (Objects.isNull(param)) {
            throw new BizException("非法操作，参数不能为空");
        }
        SysUser currentUser = loginUtil.getCurrentUser();
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId, param.getToId());
        SysUser toUser = sysUserService.getOne(queryWrapper);
        CommentsReply commentsReply = createCommentsReply(currentUser, toUser, param);
        return commentsReplyService.save(commentsReply);
    }

    private CommentsReply createCommentsReply(SysUser currentUser, SysUser toUser, CommentsReplyParam param) {
        CommentsReply reply = new CommentsReply();
        reply.setCommentId(param.getCommentId());
        reply.setFromAvatar(currentUser.getAvatar());
        reply.setToAvatar(toUser.getAvatar());
        reply.setContent(param.getContent());
        reply.setCreateTime(DateUtil.date());
        reply.setUpdateTime(DateUtil.date());
        reply.setFromId(currentUser.getId());
        reply.setFromName(currentUser.getUserName());
        reply.setToName(toUser.getUserName());
        reply.setToId(toUser.getId());
        return  reply;
    }

    public Boolean delete(CommentsReplyParam param) {
        if (Objects.isNull(param)) {
            throw new BizException("非法操作，参数不能为空");
        }
        Long id = param.getId();
        if (Objects.isNull(id)) {
            throw new BizException("非法操作，参数不能为空");
        }
        return commentsReplyService.removeById(id);
    }
}
